package com.tjsinfo.tjsoa.system.action;

import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.json.JSONArray;
import org.json.JSONObject;

import com.tjsinfo.tjsoa.system.bo.IDepartmentBo;
import com.tjsinfo.tjsoa.system.vo.TSystemDepartment;

public class DepartmentTreeZhiShuAction  extends Action{
	private IDepartmentBo departmentBo;
	
	
	
	public IDepartmentBo getDepartmentBo() {
		return departmentBo;
	}

	public void setDepartmentBo(IDepartmentBo departmentBo) {
		this.departmentBo = departmentBo;
	}

	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		JSONArray array = new JSONArray();
		String parentid = request.getParameter("node");
		List<TSystemDepartment> list = departmentBo.selectByParentID(parentid);
		for (Iterator iterator = list.iterator(); iterator.hasNext();) {
			TSystemDepartment systemDepartment = (TSystemDepartment) iterator.next();
			JSONObject object = new JSONObject();
			if(systemDepartment.getIsZhiShu() == null){
				object.put("id",  systemDepartment.getId().trim());
				object.put("text", systemDepartment.getDeptName());
				if (departmentBo.isParentDepartment(systemDepartment.getId())) {
					object.put("leaf", false);
				} else {
					object.put("leaf", true);
				}
				array.put(object);
			}
			
		}
		response.getWriter().print(array);
		return null;
	}
	
	
}
